/// <summary>
/// 设置连接状态
/// </summary>
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using static UnityEngine.Material;

public class connectState : MonoBehaviour
{
    public enum CONNECTION
    {
        CONNECTED = 1,
        DISCONNECT = 0,
    }

    public RawImage led;
    public Dropdown comList;
    public Text stateText;
    public CONNECTION state = CONNECTION.DISCONNECT;
    public serial serial;
    public Text connectButtText;
    // Start is called before the first frame update
    void Start()
    {
        setState(state);
        InvokeRepeating("checkConnection", 1, 1);

    }

    // // Update is called once per frame
    // void Update()
    // {
    // }
    void checkConnection()
    {
        if (serial.getSensorConnection() == serial.SENSOR_CONNECTION.OK)
        {
            setConnected();
        }
        else
        {
            setDisConnected();
        }


    }
    public void setState(CONNECTION state)
    {
        if (state == CONNECTION.CONNECTED)
        {
            stateText.text = "已连接";
            led.texture = Resources.Load<Texture>("picture/greenDot") as Texture;
            connectButtText.text = "断开连接";
            comList.interactable = false;

        }
        else if (state == CONNECTION.DISCONNECT)
        {
            stateText.text = "未连接";
            led.texture = Resources.Load<Texture>("picture/redDot") as Texture;
            connectButtText.text = "连接";
            comList.interactable = true;


        }
    }
    public void setConnected()
    {
        setState(CONNECTION.CONNECTED);
    }

    public void setDisConnected()
    {
        setState(CONNECTION.DISCONNECT);
    }
}
